//
//  SchoolRankingViewController.m
//  HotWord
//
//  Created by 贺 欢 on 13-3-3.
//
//

#import "MajorRankingViewController.h"
#import "SchoolSelectCell.h"
#import "MajorSelectViewController.h"
#import "HeaderTitleView.h"

@interface MajorRankingViewController ()<MajorSelectViewControllerDelegate>
{
    NSDictionary *_majorRankingDic;
    NSString *_selectMajor;
}
@property (retain, nonatomic) IBOutlet UITableView *majorTableView;

@end

@implementation MajorRankingViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        _majorRankingDic = [[NSDictionary alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Major_Ranking_List" ofType:@"plist"]];
    }
    return self;
}

- (void)dealloc
{
    [_majorTableView release];
    [_majorRankingDic release];
    [_selectMajor release];
    [super dealloc];
}

- (void)viewDidUnload
{
    [self setMajorTableView:nil];
    [super viewDidUnload];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    _majorTableView.top = self.view.height;
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [UIView animateWithDuration:0.4
                     animations:^{
                         _majorTableView.top = 0;
                         [self.view viewWithTag:10].alpha = 1;
                     }];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    NSIndexPath *selectIndexPath = [_majorTableView indexPathForSelectedRow];
    if (selectIndexPath)
    {
        [_majorTableView deselectRowAtIndexPath:selectIndexPath animated:YES];
    }
    [self.view viewWithTag:10].alpha = 0;

}

#pragma mark - IBAction

- (IBAction)tapOnBackBtn:(id)sender
{
    if (_delegate && [_delegate respondsToSelector:@selector(majorRankingViewController:didSelectMajor:)] && _selectMajor)
    {
        [_delegate majorRankingViewController:self didSelectMajor:_selectMajor];
    }
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [MAJOR_RANKING_KEYS count];
}

//- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
//{
//    HeaderTitleView *headerView = [[[NSBundle mainBundle] loadNibNamed:@"HeaderTitleView" owner:self options:nil] objectAtIndex:0];
//    headerView.titleLabel.text = @"文理学院";
//    
//    return headerView;
//}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"SchoolSelectCell";
    SchoolSelectCell *cell = (SchoolSelectCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"SchoolSelectCell" owner:self options:nil] objectAtIndex:0];
        cell.type = SchoolSelectCellTypeRanking;
        cell.titleLabel.font = [UIFont systemFontOfSize:15];
        cell.selectionStyle = UITableViewCellSelectionStyleGray;
    }
        
    [cell setTitle:[MAJOR_RANKING_KEYS objectAtIndex:indexPath.row]];
    
    return cell;
}

#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    NSString *majorType = [MAJOR_RANKING_KEYS objectAtIndex:indexPath.row];

    NSArray *majors = [_majorRankingDic objectForKey:majorType];
    
    MajorSelectViewController *majorSelectViewController = [[MajorSelectViewController alloc] initWithMajors:majors];
    majorSelectViewController.headerTitle = majorType;
    majorSelectViewController.delegate = self;
    [self.navigationController pushViewController:majorSelectViewController animated:YES];
    [majorSelectViewController release];    
}

#pragma mark - SchoolSelectViewControllerDelegate

- (void)majorSelectViewController:(MajorSelectViewController *)viewController didSelectMajor:(NSString *)major;
{
    if (_selectMajor)
    {
        RELEASE_SAFELY(_selectMajor);
    }
    
    _selectMajor = [major retain];
}


@end
